package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Operator;
import cn.wolfcode.crm.domain.Stockincome;
import cn.wolfcode.crm.domain.Supplier;
import cn.wolfcode.crm.query.ProductQueryObejct;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.query.StockOutcomeBillQueryObejct;
import cn.wolfcode.crm.service.IStockincomeService;
import cn.wolfcode.crm.service.ISupplierService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
@Controller
@RequestMapping("/stockincome")
public class StockincomeController {

    @Autowired
    private IStockincomeService stockincomeService;
    @Autowired
    private IStockincomeService bilService;
    @Autowired
    private ISupplierService supplierService;

    @RequestMapping("/getSelectList")
    @ResponseBody
    public List<Supplier> getSelectList(@ModelAttribute("qo")QueryObject qo){

        List<Supplier> list = supplierService.list();

        return list;
    }

    @RequestMapping("")
    public String view(){

        return "stockincome/list";
    }

    @RequestMapping("/list")
    @ResponseBody
    public PageResult list(@ModelAttribute("qo")StockOutcomeBillQueryObejct qo){

        PageResult result = stockincomeService.queryForList(qo);
        return result;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id){
        JsonResult result = new JsonResult();
        try {

            stockincomeService.delete(id);
        }catch (Exception e){
            e.printStackTrace();
            result.setMsg("删除失败");
        }

        return result;
    }

    @RequestMapping("/getStockById")
    @ResponseBody
    public PageResult getStockById(Long id){
       PageResult result = stockincomeService.getStockById(id);

        return result;
    }
    @RequestMapping("/save")
    @ResponseBody
    public JsonResult save(Stockincome income){
        JsonResult result = new JsonResult();
        try{
            stockincomeService.save(income);
        }catch (Exception e){
            e.printStackTrace();
            result.setMsg("保存失败");
        }
        return result;
    }

    @RequestMapping("/getStockList")
    @ResponseBody
    public PageResult getStockList(){

       PageResult result = stockincomeService.getStockList();

        return result;
    }
    @RequestMapping("/getBillList")
    @ResponseBody
    public PageResult getBillList(){

       PageResult result = bilService.getBillList();

        return result;
    }

    //入库
    @RequestMapping("/audit")
    @ResponseBody
    public JsonResult audit(Long id){
        JsonResult result = new JsonResult();
        try{

            stockincomeService.audit(id);
        }catch (Exception e){
            e.printStackTrace();
            result.setMsg(e.getMessage());

        }

        return result;
    }

    //导入
    @RequestMapping("/input")
    public String input(@RequestParam(value = "file", required = false) MultipartFile file,
                      HttpServletRequest request, HttpServletResponse response) throws Exception {

        Workbook workbook = Workbook.getWorkbook(file.getInputStream());

        //获取第一个 sheet
        Sheet sheet = workbook.getSheet(0);
        Stockincome income = new Stockincome();
        System.out.println(sheet.getRows());
        System.out.println(sheet.getColumns());

        System.out.println(file);


        for(Integer i = 1;i< sheet.getRows();i++){
            Cell c1 = sheet.getCell(0,i);
            Cell c2 = sheet.getCell(2,i);
            Cell c3 = sheet.getCell(3,i);
            Cell c4 = sheet.getCell(4,i);
            Cell c5 = sheet.getCell(5,i);
            Cell c6 = sheet.getCell(6,i);
            Cell c7 = sheet.getCell(7,i);
            Cell c8 = sheet.getCell(8,i);
            Cell c9 = sheet.getCell(10,i);

            String sn = c1.getContents();
            String supplierId = c2.getContents();
            String costPrice = c3.getContents();
            String number = c4.getContents();
            String vdate = c5.getContents();
            String amountPrice = c6.getContents();
            String receipts = c7.getContents();
            String state = c8.getContents();
            String operatorId = c9.getContents();

            Supplier s = new Supplier();
            income.setSupplier(s);
            Operator o = new Operator();
            income.setOperator(o);
            income.setSn(sn);
            income.getSupplier().setId(new Long(supplierId));
            income.setCostPrice(new BigDecimal(costPrice));
            income.setNumber(new BigDecimal(number));
            income.setVdate(new Date(vdate));
            income.setAmountPrice(new BigDecimal(amountPrice));
            income.setReceipts(new BigDecimal(receipts));
            income.setState(new Integer(state));
            income.getOperator().setId(new Long(operatorId));
            System.out.println(income);
            stockincomeService.insert(income);
        }
        return "stockincome/list";

    }


    @RequestMapping("/export")
    @ResponseBody
    public void export(ProductQueryObejct qo , HttpServletResponse response) throws Exception {






//        先查询出符合条件的数据
        qo.setRows(10000);//可能分页没有100,所以这里手动设置,以便将所有符合条件的的数据都查出来
        List<Stockincome> members = stockincomeService.queryForExport(qo);
        //设置响应头
        response.setHeader("Content-Disposition", "attachment;filename=stockincome.xls");
        OutputStream outputStream = response.getOutputStream();
        //创建一个可写入的工作簿:当关闭这个工作簿时,会自动创建一个电子表格,并流向输入流,通过http从servlet发给浏览器
        WritableWorkbook workbook = Workbook.createWorkbook(outputStream);
        WritableSheet sheet = workbook.createSheet("入库单",0);
        //创建一行单元格,装列名
        //创建一系列单元格,装列数据

try {
    sheet.addCell(new Label(0, 0, "订单编码"));
    sheet.addCell(new Label(1, 0, "供应商"));
    sheet.addCell(new Label(2, 0, "供应商id"));
    sheet.addCell(new Label(3, 0, "单价"));
    sheet.addCell(new Label(4, 0, "数量"));
    sheet.addCell(new Label(5, 0, "入库时间"));
    sheet.addCell(new Label(6, 0, "合计"));
    sheet.addCell(new Label(7, 0, "实收"));
    sheet.addCell(new Label(8, 0, "入库状态"));
    sheet.addCell(new Label(9, 0, "操作人员"));
    sheet.addCell(new Label(10, 0, "操作人员id"));
    for (int i = 0; i < members.size(); i++) {
        int row = i + 1;
        sheet.addCell(new Label(0, row, members.get(i).getSn()));
        sheet.addCell(new Label(1, row, members.get(i).getSupplier().getName()));
        sheet.addCell(new Label(2, row, members.get(i).getSupplier().getId().toString()));
        sheet.addCell(new Label(3, row, members.get(i).getCostPrice().toString()));
        sheet.addCell(new Label(4, row, members.get(i).getNumber().toString()));
        sheet.addCell(new Label(5, row, members.get(i).getInputTime().toString()));
        sheet.addCell(new Label(6, row, members.get(i).getAmountPrice().toString()));
        sheet.addCell(new Label(7, row, members.get(i).getReceipts().toString()));
        sheet.addCell(new Label(8, row, members.get(i).getState().toString()));
        sheet.addCell(new Label(9, row, members.get(i).getOperator().getName()));
        sheet.addCell(new Label(10, row, members.get(i).getOperator().getId().toString()));
    }
}catch (Exception e){
    e.printStackTrace();
}
        workbook.write();
        workbook.close();
    }

    @RequestMapping("/productIncome")
    public String productIncome(){

        return "productIncome";
    }


}

